package com.jsu.springboot_medicine.controller;

import com.jsu.springboot_medicine.common.Result;
import com.jsu.springboot_medicine.common.StatusCode;
import com.jsu.springboot_medicine.entity.Message;
import com.jsu.springboot_medicine.service.MessageService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wang
 * @Date 2023/9/26 9:06
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageService messageService;

    @GetMapping("/{receiver}")
    public Result<List<Message>> getAllByReceiver(@PathVariable("receiver") int receiver) {
        List<Message> allByReceiver = messageService.getAllByReceiver(receiver);
        return new Result<List<Message>>(true, StatusCode.OK, "查询成功", allByReceiver);
    }

    //查询所有消息通知（按时间由高到低排序）
    @GetMapping("/getAll")
    public Result getAllMessage(){
        List<Message> messages = messageService.getAllMessage();
        return new Result(true, StatusCode.OK, "查询成功", messages);
    }
    //根据消息标题关键字查找消息通知（模糊查询）
    @GetMapping("/selectByTitle/{title}")
    public Result selectByTitle(@PathVariable("title") String title){
        List<Message> messages = messageService.selectByTitle(title);
        return new Result(true, StatusCode.OK, "查询成功", messages);
    }
    //根据消息发布者查找消息通知
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable("name") String name){
        List<Message> messages = messageService.selectByName(name);
        return new Result(true, StatusCode.OK, "查询成功", messages);
    }
    //查询已读或未读消息
    @GetMapping("/selectByStatus/{status}")
    public Result selectByStatus(@PathVariable("status") Integer status){
        List<Message> messages = messageService.selectByStatus(status);
        return new Result(true, StatusCode.OK, "查询成功", messages);
    }
    //获取已读、未读消息数量
    @GetMapping("/getReadNum")
    public Result getReadNum(){
        int num = messageService.getReadNum();
        return new Result(true, StatusCode.OK, "查询成功", num);
    }
    @GetMapping("/getUnreadNum/{id}")
    public Result getUnreadNum(@PathVariable("id") int id){
        int num = messageService.getUnreadNum(id);
        return new Result(true, StatusCode.OK, "查询成功", num);
    }

    //标记消息为已读
    @PostMapping("/updateStatus/{id}")
    public Result updateStatus(@PathVariable("id") int id) {
        messageService.updateStatus(id);
        return new Result(true, StatusCode.OK, "修改成功",null);
    }
    //添加消息通知
    @PostMapping("/add")
    public Result addMessage(@RequestBody Message message) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        message.setSenderName(name);
        messageService.addMessage(message);
        return new Result(true, StatusCode.OK, "添加成功");
    }
    //修改消息通知
    @PutMapping("/update")
    public Result update(@RequestBody Message message) {
        messageService.updateMessage(message);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    //删除消息通知
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") int id) {
        messageService.deleteMessage(id);
        return new Result(true, StatusCode.OK, "删除成功",null);
    }
}
